//
//  ActivityPhotoCell.m
//  SXH
//
//  Created by Ryan on 15/12/9.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "ActivityPhotoCell.h"
#import "AcitvityPhotoCollectionCell.h"
#import "UIImageView+AFNetworking.h"
#define Distance  6
#define Edge  6
#define cHeight ([UIScreen mainScreen].bounds.size.width-Distance*2-Edge*2)*109/80/3
#define cWidth ([UIScreen mainScreen].bounds.size.width-Distance*2-Edge*2)/3.0


@implementation ActivityPhotoCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)layoutSubviews
{
    self.backgroundColor = [UIColor clearColor];
}

- (void)spotPictureCellCreateNinePicView:(NSArray*)imageArray
{

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.photoArray count];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"AcitvityPhotoCollectionCell";
    AcitvityPhotoCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell initwithModel:[self.photoArray objectAtIndex:indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(cWidth, cWidth);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(Distance, Edge, Distance, Edge);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
    if ([_delegate respondsToSelector:@selector(selectAtItem:)]) {
        [_delegate selectAtItem:[self.photoArray objectAtIndex:indexPath.row]];
    }
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)imageClick:(UITapGestureRecognizer *)tap
{

}

- (void)initMainView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setMinimumLineSpacing:6];
    [flowLayout setMinimumInteritemSpacing:6];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    static NSString * CellIdentifier = @"AcitvityPhotoCollectionCell";
    [self.collectionView registerClass:[AcitvityPhotoCollectionCell class]forCellWithReuseIdentifier:CellIdentifier];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).width.insets(UIEdgeInsetsMake(0, 0,0, 0));
    }];
    self.collectionView.backgroundColor = [UIColor clearColor];
}

- (void)setPhotoArray:(NSMutableArray *)photoArray
{
    _photoArray = photoArray;
    [self initMainView];
    [self.collectionView reloadData];
    
}


@end
